<?php
if(!defined("YUEXUAN_REAL_ROOT_PATH")){
	exit("Access Denied.");
}

/**
 * 日志类
 * @author tongjun
 *
 */
class ext_logger {
	// 是否输出日志
	private $isOutputlog = RECORD_LOG;
	private static $_instance;
	
	private function __construct() {
	}
	private function __clone() {
		 
	}
	/**
	 * 单例模式下取得系统设置
	 * @return array
	 */
	public static function getInstance() {
		if(!(self::$_instance instanceof self)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public function debug($msg){
		//$this->output(LOG_DEBUG, $msg);
		$this->output(LOG_ERR, $msg);
	}
	
	public function warn($msg){
		//$this->output(LOG_WARNING, $msg);
		$this->output(LOG_ERR, $msg);
	}
	
	public function error($msg){
		$this->output(LOG_ERR, $msg);
	}
	
	// 将数组或object转成string
	private function object2str($msg){
		if(is_array($msg) || is_object($msg)){
			$msg = print_r($msg, true);
		}
		return $msg;
	}
	
	// 按级别输出日志
	private function output($loglevel, $msg){
		if(!$this->isOutputlog){
			return false;
		}
		$msg = $this->object2str($msg);
		// 追踪调用者
		$backTrace = debug_backtrace();
		$msg .= " in ".$backTrace[1]['file']." on line ".$backTrace[1]['line'].".";
		//syslog($loglevel, $msg);
		$logDir = YUEXUAN_REAL_ROOT_PATH."/".LOG_DIR."/";
		if(!file_exists($logDir)){
			mkdir($logDir, 0755, true);
		}
		$msg = "[".date("Y-m-d H:i:s")."]".$msg."\r\n";
		error_log($msg, 3, $logDir.date("Y-m-d").".log");
	}
}

?>
